package com.gdufe.seckill.mq;

import com.gdufe.seckill.entity.RelationEntity;
import com.gdufe.seckill.entity.SessionEntity;
import com.gdufe.seckill.service.RelationUserService;
import com.gdufe.seckill.service.SessionUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.baomidou.mybatisplus.extension.toolkit.Db.updateById;

@Slf4j
@Component
@RocketMQMessageListener(nameServer = "${rocketmq.name-server}", topic = "seckillstock",
        consumerGroup = "mall-seckill", messageModel = MessageModel.BROADCASTING)
public class SeckillMQListener implements RocketMQListener<Map<String, String>> {
    @Autowired
    private RelationUserService relationUserService;
    @Override
    public void onMessage(Map<String, String> Map) {
        log.info("接收到扣减消息：" );
        Long id= Long.valueOf(Map.get("seckillId"));
        int productnum= Integer.parseInt(Map.get("productNum"));
        RelationEntity relation = relationUserService.getById(id);
        relation.setSeckillCount(relation.getSeckillCount()-productnum);

        updateById(relation);
    }
}
